package hubsniffer;
import hubsniffer.conn.Connection;
import hubsniffer.conn.HubInfoConnection;
import libjdc.dc.client.HubClient;
import hubsniffer.util.Host;
import libjdc.dc.client.User;
import libjdc.dc.log.Log;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Timer;
import java.util.TimerTask;
/*
 * ConnectHub.java
 *
 * Created on 22 de Mar�o de 2006, 14:50
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author botelhodaniel
 */
public class ConnectHubThread extends Thread {
    
    private Connection hubConnection;
    private long delay;
    private boolean infinite;
    
    /** Creates a new instance of ConnectHub */
    public ConnectHubThread(Connection hubConnection, long delay){
        super(hubConnection);
        this.hubConnection = hubConnection;
        this.delay = delay;
        this.infinite = false;
    }
    
    public ConnectHubThread(Connection hubConnection){
        super(hubConnection);
        this.hubConnection = hubConnection;
        this.infinite = true;
    }
    
    
    /* (non-Javadoc)
     * @see java.lang.Thread#start()
     */
    @Override
    public synchronized void start() {
        // TODO Auto-generated method stub
        if(!this.infinite)
            hubConnection.setTimer(new HubDisconnect(getHubConnection(), delay));
        super.start();
    }
    
    
    public static void main(String []args) throws InterruptedException, ClassNotFoundException, SQLException, FileNotFoundException{
        Log.setOutPutStream(System.out);
        Log.setErrOutPutStream(System.err);

        DCHub hub = new DCHub(7,"dance-club.no-ip.org", 411,1);
        User me = new User("[PT]sniffer","{HubSniffer Project} <++ V:0.698,M:A,H:1/0/0,S:3>", "DSL","email@com.c", 104219547999L);
        
        me.setHost(Host.getHostIP());
        me.getUserInfo().setDescription("http://sghdc.no-ip.org$http://sghdc.no-ip.org/download?file=PublicHubList$.xml$ $http://sghdc.no-ip.org/viewhub?hub_id="+hub.getHub_id()+"$HubSniffer Project: http://sghdc.no-ip.org/");
        
        long delay = 20000L;
        HubClient.addHubClient(0,"PtokaX","","","Pk=PtokaX");
        HubClient.addHubClient(4,"Verlihub","","","verlihub");
        HubClient.addHubClient(5,"YnHub","","","Pk=YnHub");
        Log.out.println("Bla");
       ConnectHubThread conn = new ConnectHubThread(new HubInfoConnection(hub,me),delay);
        conn.start();
        Log.out.println("Bla2");
        conn.join();
        Log.out.println("Bla3");
        
        hub.getHubSession().print();

        
    }
    
    public Connection getHubConnection() {
        return hubConnection;
    }
}
class HubDisconnect extends TimerTask{
    
    private Timer timer;
    private Connection hubConnection;
    
    public HubDisconnect(Connection hubConnection,long time){
        
        this.hubConnection = hubConnection;
        this.timer = new Timer(hubConnection.toString());
        this.timer.schedule(this,time);
    }
    public boolean cancel(){
        timer.cancel();
        return super.cancel();
    }
    
        /* (non-Javadoc)
         * @see java.util.TimerTask#run()
         */
    public void run() {
        //Sniffer.out.println(this.toString());
        // TODO Auto-generated method stub
        Log.out.println("Vai mandar encerrar o Hub: "+hubConnection.getHub().getHost());
        try {
            hubConnection.disconnect();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(Log.err);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(Log.err);
        }
        
        timer.cancel();
        
    }
    
}
